// pages/classify/classify.js
//获取应用实例
const app = getApp();
const api = require('../../utils/api');
const constant = require('../../utils/constant');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //获取全局变量 导航栏的高度statusBarHeight
    statusBarHeight: app.globalData.statusBarHeight,
    // 胶囊
    jiaonang: app.globalData.jiaonang, 
    navigationHeight: app.globalData.navigationHeight,
    // 底部菜单高度
    tabbarHeight: app.globalData.tabbarHeight,
    BASEURLWWW:constant.BASEURLWWW,
    // search高度
    searchHeight: 0,
    // 分类
    scrollSideId: '',
    scrollMainId: '',
    top: 0,
    goodsCircleListData: [],
    classify:[
      {id:1,title: '新品首发',cover: '../../assets/img/flash.jpg',content:[
        {id: 1,title: '新品1',list:[
          {id:1,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:2,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:3,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:4,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:5,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:6,title: '套组',cover:'../../assets/img/hot1.png'},
        ]},
        {id: 2,title: '新品2',list:[
          {id:1,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:2,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:3,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:4,title: '套组',cover:'../../assets/img/hot1.png'},
        ]}
      ]},
      {id:2,title: '飞天茅台',cover: '../../assets/img/flash.jpg',content:[
        {id: 1,title: '新品1',list:[
          {id:1,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:2,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:3,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:4,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:5,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:6,title: '套组',cover:'../../assets/img/hot1.png'},
        ]}
      ]},
      {id:3,title: '老茅台',cover: '../../assets/img/flash.jpg',content:[
        {id: 1,title: '新品1',list:[
          {id:1,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:2,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:3,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:4,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:5,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:6,title: '套组',cover:'../../assets/img/hot1.png'},
        ]}
      ]},
      {id:4,title: '各式茅台',cover: '../../assets/img/flash.jpg',content:[
        {id: 1,title: '新品1',list:[
          {id:1,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:2,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:3,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:4,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:5,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:6,title: '套组',cover:'../../assets/img/hot1.png'},
        ]}
      ]},
      {id:5,title: '名优老酒',cover: '../../assets/img/flash.jpg',content:[
        {id: 1,title: '新品1',list:[
          {id:1,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:2,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:3,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:4,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:5,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:6,title: '套组',cover:'../../assets/img/hot1.png'},
        ]}
      ]},
      {id:6,title: '畅销名酒',cover: '../../assets/img/flash.jpg',content:[
        {id: 1,title: '新品1',list:[
          {id:1,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:2,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:3,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:4,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:5,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:6,title: '套组',cover:'../../assets/img/hot1.png'},
        ]}
      ]},
      {id:7,title: '世界名庄',cover: '../../assets/img/flash.jpg',content:[
        {id: 1,title: '新品1',list:[
          {id:1,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:2,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:3,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:4,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:5,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:6,title: '套组',cover:'../../assets/img/hot1.png'},
        ]}
      ]},
      {id:8,title: '常樂精选',cover: '../../assets/img/flash.jpg',content:[
        {id: 1,title: '新品1',list:[
          {id:1,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:2,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:3,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:4,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:5,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:6,title: '套组',cover:'../../assets/img/hot1.png'},
        ]}
      ]},
      {id:9,title: '知名烈酒',cover: '../../assets/img/flash.jpg',content:[
        {id: 1,title: '新品1',list:[
          {id:1,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:2,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:3,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:4,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:5,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:6,title: '套组',cover:'../../assets/img/hot1.png'},
        ]}
      ]},
      {id:10,title: '日本清酒',cover: '../../assets/img/flash.jpg',content:[
        {id: 1,title: '新品1',list:[
          {id:1,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:2,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:3,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:4,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:5,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:6,title: '套组',cover:'../../assets/img/hot1.png'},
        ]}
      ]},
      {id:11,title: '精酿啤酒',cover: '../../assets/img/flash.jpg',content:[
        {id: 1,title: '新品1',list:[
          {id:1,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:2,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:3,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:4,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:5,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:6,title: '套组',cover:'../../assets/img/hot1.png'},
        ]}
      ]},
      {id:12,title: '营养滋补',cover: '../../assets/img/flash.jpg',content:[
        {id: 1,title: '新品1',list:[
          {id:1,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:2,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:3,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:4,title: '套组',cover:'../../assets/img/hot1.png'},
          {id:5,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:6,title: '套组',cover:'../../assets/img/hot1.png'},
        ]}
      ]},
      {id:13,title: '美酒周边',cover: '../../assets/img/flash.jpg',content:[
        {id: 1,title: '新品1',list:[
          {id:1,title: '单瓶',cover:'../../assets/img/hot1.png'},
          {id:2,title: '套组',cover:'../../assets/img/hot1.png'}
        ]}
      ]}
    ],
    isSelect: 0,
    // 选项卡状态
    tabStatus: 'classify',
    // 品牌列表数据
    brandListData: [],
    isBrandSelect: 0,
    // 品牌列表chidrens数据
    brandChildrenListData: [],
    brandSearch: 'all'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    // 获取商品圈数据
    this.getGoodsCircleList();
    // 获取搜索板块高度
    wx.createSelectorQuery().select('#search').boundingClientRect(rect=>{  
      let height = rect.height;  
      that.setData({
        searchHeight: rect.height
      })
    }).exec(); 
    // 获取品牌列表
    this.getBrandListData();
  },
  // 获取商品圈数据
  getGoodsCircleList:function(){
    let top2 = [];
    api.goodsCircleList().then(res => {
      if(res.code == 0){
        this.setData({
          top:top2,
          goodsCircleListData: res.data
        })
        for (let i = 0; i < res.data.length; i++) {
          wx.createSelectorQuery().select('#classifyMain' + i).boundingClientRect(function (rect) {
            let isTop = Number(rect.top);
            top2.push(isTop);
           }).exec();
        }
        this.setData({
          top:top2,
        })
      }
    })
  },
  // 获取品牌列表数据
  getBrandListData:function(){
    api.getBrandListTree().then(res => {
      if(res.code == 0){
        this.setData({
          isBrandSelect: res.data[0].brand_id,
          brandListData: res.data,
          brandChildrenListData: res.data[0].childrens
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 点击搜索框
  jumpToSearch: function() {
    wx.navigateTo({
       url: '/pages/search/search'
     })
   },
  // 选项卡点击
  tabClick:function(e){
    let status = e.currentTarget.dataset.status;
    this.setData({
      tabStatus: status
    })
  },
  //  侧面菜单点击切换样式
  select:function(e){
    this.setData({
      isSelect: e.target.dataset.index,
      scrollMainId: "classifyMain"+e.target.dataset.index
    })
  },
  // main滚动事件
  mainScroll:function(e){
    const scrollTop = e.detail.scrollTop;
    const length = this.data.top.length;
    for(let i = 0;i<this.data.top.length;i++){
      if (this.data.top[i] - this.data.top[0] <= scrollTop && (i < length - 1 && this.data.top[i + 1] - this.data.top[0] > scrollTop)){
        if(this.data.isSelect!=i){
          this.setData({
            isSelect: i,
            scrollSideId: 'classifySide'+i
          }); 
         }
      }
    }
  },
  // 侧面品牌点击
  chooseBrand:function(e){
    let item = e.target.dataset.item;
    this.setData({
      isBrandSelect: item.brand_id,
      brandChildrenListData: item.childrens,
      brandSearch: 'all'
    })
  },
  // 筛选点击
  brandSearchChoose:function(e){
    this.setData({
      brandSearch: e.target.dataset.value
    })
  },
  // 跳转到列表页
  jumpToList:function(e){
    let friend = {
      friend_id: e.currentTarget.dataset.item.frend_id,
      friend_title: e.currentTarget.dataset.item.frend_title
    }
    wx.navigateTo({
      url: `/pages/list/list?friend=${JSON.stringify(friend)}`,
    })
  }
})